<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>作业管理</title>
    <link rel="stylesheet" href="../../component/pear/css/pear.css"/>
    <style>
        .camera_icon {
            font-size: 18px !important;
            cursor: pointer;
        }

        .camera_active {
            color: #049590 !important;
            font-weight: bolder;
        }

        .layui-table-body::-webkit-scrollbar {
            width: 6px;
            height: 15px !important;
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">工作地点</label>
                    <div class="layui-input-inline">
                        <input type="text" name="operationLocation" placeholder="" class="layui-input"
                               autocomplete="off">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">工作状态</label>
                    <div class="layui-input-inline">
                        <select name="status" lay-verify="" lay-search="">
                            <option value="">请选择</option>
                            <option value="0">未开始</option>
                            <option value="1">进行中</option>
                            <option value="2">已结束</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="task-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>

                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <button class="pear-btn pear-btn-info pear-btn-md" id="uploadAction" style="display: none">
            <i class="layui-icon layui-icon-upload"></i>
            上传
        </button>
        <table id="task-table" lay-filter="task-table"></table>
    </div>
</div>

<script type="text/html" id="task-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
    <button class="pear-btn pear-btn-success pear-btn-md" lay-event="start">
        <i class="layui-icon layui-icon-ok"></i>
        开始
    </button>
    <button class="pear-btn pear-btn-warming pear-btn-md" lay-event="over">
        <i class="layui-icon layui-icon-close"></i>
        结束
    </button>
    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">
        <i class="layui-icon layui-icon-delete"></i>
        删除
    </button>
    <button class="pear-btn pear-btn-info pear-btn-md" lay-event="import">
        <i class="layui-icon layui-icon-upload"></i>
        导入
    </button>
</script>
<script type="text/html" id="task-bar">
    {{# if(d.zyzt!="未开始"){ }}
    <button class="pear-btn {{d.zyzt=='已结束'?'pear-btn-disable':'pear-btn-danger'}} pear-btn-sm" lay-event="stop" title="结束" {{d.zyzt=='已结束'?'disabled':''}}>
    <i class="layui-icon layui-icon-pause"></i></button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="showsf" title="算法"><i
            class="layui-icon layui-icon-chart-screen"></i></button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="show" title="查看"><i
            class="layui-icon layui-icon-eye"></i></button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" title="编辑"><i
            class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="ocr" title="OCR"><i
            class="layui-icon layui-icon-camera"></i></button>
    {{# }else{ }}
    <button class="pear-btn pear-btn-success pear-btn-sm" lay-event="start" title="开始"><i
            class="layui-icon layui-icon-play"></i></button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit" title="编辑"><i
            class="layui-icon layui-icon-edit"></i></button>
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="ocr" title="OCR"><i
            class="layui-icon layui-icon-camera"></i></button>
    {{# } }}
    <button class="pear-btn {{d.zyzt!='未开始'?'pear-btn-disable':'pear-btn-danger'}} pear-btn-sm" lay-event="remove" title="删除" {{d.zyzt!='未开始'?'disabled':''}}>
    <i class="layui-icon layui-icon-delete"></i></button>
</script>

<script type="text/html" id="start_time">
    {{layui.util.toDateString(d.startTime, 'MM-dd HH:mm') }}
</script>
<script type="text/html" id="end_time">
    {{layui.util.toDateString(d.endTime, 'MM-dd HH:mm') }}
</script>
<script type="text/html" id="cameras">
    {{# if (d.zyzt=="进行中") { }}
    {{# for (var i=0,cs=d.cameras.split(','),cn=d.cameras_name.split(',');i < cs.length;i++){}}
    <span><i id="{{ d.id }}_{{ cs[i] }}" title="{{ cn[i] }}" camera_num="{{ cs[i] }}" algorithms="{{ d.algorithms }}"
             onclick="camera_click(this);" class="layui-icon layui-icon-video camera_icon"></i></span>
    {{# } } }}
</script>

<script src="../../component/layui/layui.js"></script>
<script src="../../component/pear/pear.js"></script>
<script>
    let CAMERA_MANAGE_PATH = "${ctxPath}/api/camera/";

    layui.use(['table', 'form', 'jquery', 'common', 'toast', 'upload', 'element'], function () {
        let table = layui.table;
        let form = layui.form;
        let upload = layui.upload;
        let element = layui.element
        let $ = layui.jquery;
        let common = layui.common;
        var toast = layui.toast;

        let VIEW_PATH = "${ctxPath}/busOperationplan/view/";
        let DATA_PATH = "${ctxPath}/busOperationplan/data/";
        let MODULE_PATH = "${ctxPath}/ocr/";


        let cols = [[
            {type: 'checkbox'},
            {title: '工作内容', field: 'jobContent', align: 'center', width: 200},
            {title: '工作地点', field: 'operationLocation', align: 'center', width: 100},
            {title: '开始时间', field: 'startTime', align: 'center', width: 100, templet: '#start_time'},
            {title: '结束时间', field: 'endTime', align: 'center', width: 100, templet: '#end_time'},
            {title: '工作性质', field: 'workingType', align: 'center', width: 120},
            {title: '是否停电', field: 'sfdd', align: 'center', width: 85},
            {title: '作业人员', field: 'zyryid', align: 'center', width: 140},
            {title: '安全监护人', field: 'aqjdr', align: 'center', width: 95},
            {title: '专责监护人', field: 'zzjhr', align: 'center', width: 95},
            {title: '工作负责人', field: 'gzfzr', align: 'center', width: 95},
            {title: '作业风险等级', field: 'zyfxdj', align: 'center', width: 110},
            {title: '电网风险等级', field: 'dwfxdj', align: 'center', width: 110},
            {title: '智能管控', field: 'cameras', align: 'left', width: 100, templet: '#cameras'},
            {title: '状态', field: 'zyzt', align: 'center', width: 80},
            {title: '操作', toolbar: '#task-bar', align: 'center', width: 240}
        ]];

        table.render({
            elem: '#task-table',
            url: DATA_PATH + 'listplan',
            page: true,
            cols: cols,
            curr: 1,
            skin: 'line',
            toolbar: '#task-toolbar',
            defaultToolbar: "",
            height: 'full-180',
            done: function (res, curr, count) {
                if (res && res.data && res.data.length) {
                    $.ajax({
                        url: CAMERA_MANAGE_PATH + "getWatchList",
                        dataType: 'json',
                        type: 'get',
                        success: function (result) {
                            if (result.code == 0) {
                                if (result.data && result.data != "") {
                                    let camera_online = result.data.ONLINE_CAMERA;
                                    if (camera_online && camera_online.length > 0) {
                                        let camera;
                                        for (var i = 0; i < camera_online.length; i++) {
                                            for (var j = 0; j < res.data.length; j++) {
                                                camera = (res.data[j].cameras || "").split(",");
                                                for (var k = 0; k < camera.length; k++) {
                                                    if (camera[k] == camera_online[i]) {
                                                        $("#" + res.data[j].id + "_" + camera_online[i]).addClass("camera_active");
                                                        camera_online.splice[i];
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
            }
        });

        table.on('tool(task-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.saveOrUpdate(obj, '作业计划-修改', 2);
                /*if (obj.data.status==='0'){
                    window.saveOrUpdate(obj,'作业计划-修改',2);
                }else{
                    layer.msg("作业状态"+obj.data.zyzt+",无法修改", {icon: 3, time: 1000});
                }*/
            } else if (obj.event === 'show') {
                window.saveOrUpdate(obj, '作业计划-查看', 3);
            } else if (obj.event === 'start') {
                if (obj.data.status === '0') {
                    start(obj.data.id);
                } else {
                    layer.msg("作业状态非未开始状态！", {icon: 3, time: 1000});
                }
            } else if (obj.event === 'stop') {
                window.over(obj.data.id);
            } else if (obj.event === 'ocr') {
                window.upload(obj.data.id);
            } else if (obj.event==='showsf'){
                window.showsf(obj.data.id);
            }
        });

        table.on('toolbar(task-table)', function (obj) {
            if (obj.event === 'add') {
                window.saveOrUpdate(null, '作业计划-新增', 1);
            } else if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchRemove') {
                window.batchRemove(obj);
            } else if (obj.event === 'over') {
                window.overAll(obj);
            } else if (obj.event === 'start') {
                window.startAll(obj);
            } else if (obj.event === 'import') {
                $("#uploadAction").click();
            }
        });

        form.on('submit(task-query)', function (data) {

            table.reload('task-table', {
                where: data.field, page: {curr: 1}
            })
            return false;
        });


        window.saveOrUpdate = function (obj, title, type) {
            layer.open({
                type: 2,
                title: title,
                shade: 0.1,
                id: 'taskinfo',
                area: [common.isModile() ? '100%' : '1200px', common.isModile() ? '100%' : '670px'],
                content: VIEW_PATH + 'saveOrUpdate' + "?type=" + type + (obj ? "&id=" + obj.data.id : "")
            });
        }

        window.remove = function (obj) {
            if (obj.data.status === '1') {
                layer.msg("作业状态" + obj.data.zyzt + ",无法删除", {icon: 3, time: 1000});
            } else {
                layer.confirm('确定要删除该条记录吗', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    layer.close(index);
                    let loading = layer.load();
                    $.ajax({
                        url: DATA_PATH + "remove?ids=" + obj.data['id'],
                        dataType: 'json',
                        type: 'post',
                        success: function (result) {
                            layer.close(loading);
                            if (result.code == 0) {
                                layer.msg("操作成功", {
                                    icon: 1,
                                    time: 1000
                                }, function () {
                                    obj.del();
                                    table.reload('task-table');
                                });
                            } else {
                                layer.msg("操作失败", {
                                    icon: 2,
                                    time: 1000
                                });
                            }
                        }
                    })
                });
            }
        }
        window.showsf=function (checkIds){
            layer.open({
                type: 2,
                title: "已开启算法",
                shade: 0.1,
                area: [common.isModile() ? '100%' : '600px', common.isModile() ? '100%' : '400px'],
                content: VIEW_PATH + "sf?ids=" + checkIds+"&type="+1,
            });
        }

        window.batchRemove = function (obj) {
            var checkIds = common.checkField(obj, 'id');
            var zyzt = common.checkField(obj, 'status');
            if (zyzt.indexOf("1") != -1) {
                layer.msg("选中记录里包含作业状态进行中", {
                    icon: 0,
                    time: 1000
                });
                return false;
            }
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            layer.confirm('确定要删除这些选项', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: DATA_PATH + "remove?ids=" + checkIds,
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 0) {
                            layer.msg("操作成功", {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('task-table');
                            });
                        } else {
                            layer.msg("操作失败", {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.upload = function(id) {
            layer.open({
                type: 2,
                title: '工作票照片上传',
                shade: 0.5,
                area: ['1000px', '600px'],
                content: MODULE_PATH + 'upload?id=' + id
            });
        }


        var uploadInst = upload.render({
            elem: '#uploadAction' //绑定元素
            , url: DATA_PATH + 'importData' //上传接口
            , accept: 'file'
            , exts: 'xls|xlsx|xlsm'
            , before: function (obj) {
                layer.load(); //上传loading
            }
            , done: function (res) {
                console.log(res);
                //上传完毕回调
                if (res.code == 0) {
                    layer.closeAll('loading'); //关闭loading
                    if (res.data.code==200){
                        layer.msg("操作成功，共计导入条数：" + res.data.size, {
                            icon: 1,
                            time: 2000
                        }, function () {
                            table.reload('task-table');
                        });
                    }else{
                        layer.msg("操作失败，提示：" + res.data.msg, {
                            icon: 2,
                            time: 4000
                        }, function () {

                        });
                    }
                }
            }
            , error: function () {
                layer.closeAll('loading'); //关闭loading
                //请求异常回调
                layer.msg("操作失败", {
                    icon: 2,
                    time: 1000
                });
            }
        });

        window.refresh = function (param) {
            table.reload('task-table');
        }

        window.overAll = function (obj) {
            var checkIds = common.checkField(obj, 'id');
            var zyzt = common.checkField(obj, 'status');
            if (zyzt.indexOf("0") != -1 || zyzt.indexOf("2") != -1) {
                layer.msg("请选择作业状态进行中的记录", {
                    icon: 0,
                    time: 1000
                });
                return false;
            }
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }
            layer.confirm('确定要结束这些计划', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: DATA_PATH + "over?ids=" + checkIds,
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 0) {
                            layer.msg("操作成功", {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('task-table');
                            });
                        } else {
                            layer.msg("操作失败", {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.over = function (checkIds) {
            layer.confirm('确定要结束该计划', {
                icon: 3,
                title: '提示'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: DATA_PATH + "over?ids=" + checkIds,
                    dataType: 'json',
                    type: 'post',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 0) {
                            layer.msg("操作成功", {
                                icon: 1,
                                time: 1000
                            }, function () {
                                table.reload('task-table');
                            });
                        } else {
                            layer.msg("操作失败", {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.startAll = function (obj) {
            var checkIds = common.checkField(obj, 'id');
            var zyzt = common.checkField(obj, 'status');
            if (zyzt.indexOf("1") != -1 || zyzt.indexOf("2") != -1) {
                layer.msg("请选择作业状态未开始的记录", {
                    icon: 0,
                    time: 1000
                });
                return false;
            }
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 0,
                    time: 1000
                });
                return false;
            }
            layer.open({
                type: 2,
                title: "作业计划-开启违章识别",
                shade: 0.1,
                area: [common.isModile() ? '100%' : '600px', common.isModile() ? '100%' : '500px'],
                content: VIEW_PATH + "sf?ids=" + checkIds,
            });
        }

        window.start = function (checkIds) {
            layer.open({
                type: 2,
                title: "作业计划-开启违章识别",
                shade: 0.1,
                area: [common.isModile() ? '100%' : '600px', common.isModile() ? '100%' : '500px'],
                content: VIEW_PATH + "sf?ids=" + checkIds,
            });
        }

        window.camera_click = function (obj) {
            if ($(obj).hasClass("camera_active")) { // 移除
                layer.confirm('确定要关闭该摄像头的智能管控吗？', {
                    icon: 3,
                    title: '提示'
                }, function (index) {
                    $.ajax({
                        url: CAMERA_MANAGE_PATH + "watchControl",
                        dataType: 'json',
                        data: {
                            add: "",
                            remove: $(obj).attr("camera_num"),
                            sfId: ""
                        },
                        type: 'get',
                        success: function (result) {
                            if (result) {
                                if (result.data.REMOVED_CAMERA.length > 0) {
                                    layer.close(index);
                                    $(obj).removeClass("camera_active");
                                }
                            }
                        }
                    });
                });
            } else { // 新增
                $.ajax({
                    url: CAMERA_MANAGE_PATH + "watchControl",
                    dataType: 'json',
                    data: {
                        add: $(obj).attr("camera_num"),
                        remove: "",
                        sfId: $(obj).attr("algorithms")
                    },
                    type: 'get',
                    success: function (result) {
                        if (result && result.code == 0) {
                            if (result.data.ADDED_CAMERA.length > 0) {
                                $(obj).addClass("camera_active");
                                toast.success({title: "系统消息", message: result.message, position: 'topRight'});
                            } else {
                                toast.error({title: "系统消息", message: "操作失败", position: 'topRight'});
                            }
                        } else {
                            toast.error({title: "系统消息", message: result.message, position: 'topRight'});
                        }
                    }
                });
            }
        }

        window.uploadVideo = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            console.log(data);
            if (data.length != 1) {
                layer.msg("请选择一条要操作的记录！", {icon: 3, time: 1000});
                return false;
            }
            let planid = data[0].id;
            console.log(planid);
            layer.open({
                type: 2,
                title: '离线监控视频上传',
                shade: 0.5,
                area: ['900px', '500px'],
                content: VIEW_PATH + 'upload?planid=' + planid,
            });
        }
    });
</script>
</body>
</html>
